package net.bezrabotnih.cabinet.client.place;

import java.util.ArrayList;
import java.util.List;

import org.apache.bcel.generic.RETURN;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceChangeEvent;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.HasConstrainedValue;

public class MainMenuItemPicker implements ValueChangeHandler<MenuItemPlace>, PlaceChangeEvent.Handler {
	
	private PlaceController placeController;
	private HasConstrainedValue<MenuItemPlace> view;
	private List<MenuItemPlace> performerMenuItemPlaces;
	private List<MenuItemPlace> providerMenuItemPlaces;
	private List<MenuItemPlace> currentMenuItemPlaces;
	private boolean isPerformerMenu;

	public MainMenuItemPicker(PlaceController placeController) {
		this.placeController=placeController;
		initPerformerMenuItemPlaces();
		initProviderMenuItemPlaces();
	}

	@Override
	public void onPlaceChange(PlaceChangeEvent event) {
		System.out.println("placeChange");
		if (event.getNewPlace() instanceof ProfilePlace){
			ProfilePlace newPlace=(ProfilePlace)event.getNewPlace();
			if (newPlace.getToken().equals("provider")&&isPerformerMenu){
				setProviderView();
//				System.out.println("provider");
			}else if (newPlace.getToken().equals("performer")&&!isPerformerMenu){
				setPerformerView();
//				System.out.println("performer");
			}
		}
		
		
		if (currentMenuItemPlaces!=null){
			int index=currentMenuItemPlaces.indexOf(event.getNewPlace());
			if (index>=0){
				MenuItemPlace menuItemPlace=currentMenuItemPlaces.get(index);
				if (menuItemPlace!=null) view.setValue(((MenuItemPlace)event.getNewPlace()), false);
			}
		}
		
		
	}

	@Override
	public void onValueChange(ValueChangeEvent<MenuItemPlace> event) {
		placeController.goTo(event.getValue());
		
	}
	
	public HandlerRegistration register(EventBus eventBus, HasConstrainedValue<MenuItemPlace> view) {
		this.view = view;
		final HandlerRegistration placeRegistration = eventBus.addHandler(PlaceChangeEvent.TYPE, this);
		final HandlerRegistration viewRegistration = view.addValueChangeHandler(this);

		return new HandlerRegistration() {
			public void removeHandler() {
				placeRegistration.removeHandler();
				viewRegistration.removeHandler();
				MainMenuItemPicker.this.view = null;
			}
		};
	}
	
	
	public void setProviderView(){
		currentMenuItemPlaces=providerMenuItemPlaces;
		view.setAcceptableValues(providerMenuItemPlaces);
		isPerformerMenu=false;
	}
	
	public void setPerformerView(){
		currentMenuItemPlaces=performerMenuItemPlaces;
		view.setAcceptableValues(performerMenuItemPlaces);
		isPerformerMenu=true;
	}
	
	private void initProviderMenuItemPlaces(){
		providerMenuItemPlaces=new ArrayList<MenuItemPlace>();
		providerMenuItemPlaces.add(new ProviderMainPlace());
		providerMenuItemPlaces.add(new ProviderJobsPlace());
		providerMenuItemPlaces.add(new CreateJobPlace("Создать работу","createJob"));
		providerMenuItemPlaces.add(new ProfilePlace("Профайл", "profile"));
	}
	
	private void initPerformerMenuItemPlaces(){
		performerMenuItemPlaces=new ArrayList<MenuItemPlace>();
		performerMenuItemPlaces.add(new PerformerMainPlace());
		performerMenuItemPlaces.add(new PerformerJobsPlace());
		performerMenuItemPlaces.add(new FindJobPlace("Поиск работы", "findJob"));
		performerMenuItemPlaces.add(new ProfilePlace("Профайл", "ПРофайл"));
		
		
		
	}
	
	
	
	
}
